﻿using System;
using System.Net;
using System.Web;

namespace Cxuu.Common
{
    /// <summary>
    /// 提供与Web请求时可使用的工具类，包括Url解析、Url/Html编码、获取IP地址、返回http状态码
    /// </summary>
    public static class WebUtility
    {
        public static readonly string HtmlNewLine = "<br />";

        /// <summary>
        /// 将IPv4格式的字符串转换为int型表示
        /// </summary>
        /// <param name="strIPAddress"></param>
        /// <returns></returns>
        public static uint IPToNumber(string strIPAddress)
        {
            if (!string.IsNullOrEmpty(strIPAddress)
                && IPAddress.TryParse(strIPAddress, out IPAddress ip))
            {
                byte[] bytes = ip.GetAddressBytes();
                Array.Reverse(bytes);
                return BitConverter.ToUInt32(bytes, 0);
            }
            return 0;
        }

        /// <summary>
        /// 整数转换为IP地址
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string NumberToIPStr(uint number)
        {
            uint netInt = (uint)IPAddress.HostToNetworkOrder((Int32)number);
            IPAddress ipaddr = new(netInt);
            return ipaddr.ToString();
        }


        /// <summary>
        /// 获取根域名 
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="domainRules">域名规则  ".com.cn", ".edu.cn", ".net.cn",</param>
        /// <returns>例如：http://www.google.co.uk，应该返回google.co.uk，http://www.test.googlepages.com,应该返回googlepages.com</returns>
        public static string GetServerDomain(Uri uri, string[] domainRules)
        {
            if (uri == null)
            {
                return string.Empty;
            }
            string str = uri.Host.ToString().ToLower();
            if (str.IndexOf('.') <= 0)
            {
                return str;
            }
            string[] strArray = str.Split(new char[] { '.' });
            string s = strArray.GetValue((strArray.Length - 1)).ToString();
            if (int.TryParse(s, out _))
            {
                return str;
            }

            string str1 = string.Empty;
            for (int i = 0; i < domainRules.Length; i++)
            {
                if (str.EndsWith(domainRules[i].ToLower()))
                {
                    string oldValue = domainRules[i].ToLower();
                    string str2 = str.Replace(oldValue, "");
                    if (str2.IndexOf('.') > 0)
                    {
                        string[] strArray2 = str2.Split(new char[] { '.' });
                        return (strArray2.GetValue((int)(strArray2.Length - 1)).ToString() + oldValue);
                    }
                    return (str2 + oldValue);
                }
                str1 = str;
            }
            return str1;
        }

        /// <summary>
        /// 获取带传输协议的完整的主机地址 
        /// </summary>
        /// <param name="httpRequest"></param>
        /// <returns></returns>
        public static string HostPath(Microsoft.AspNetCore.Http.HttpRequest httpRequest)
        {
            if (httpRequest == null)
            {
                return string.Empty;
            }
            return (httpRequest.Scheme + Uri.SchemeDelimiter + httpRequest.Host.Value);
        }



        /// <summary>
        /// html解码 
        /// </summary>
        /// <param name="rawContent"></param>
        /// <returns></returns>
        public static string HtmlDecode(string rawContent)
        {
            if (!string.IsNullOrEmpty(rawContent))
            {
                return HttpUtility.HtmlDecode(rawContent);
            }
            return rawContent;
        }

        /// <summary>
        /// html编码 
        /// </summary>
        /// <param name="rawContent"></param>
        /// <returns></returns>
        public static string HtmlEncode(string rawContent)
        {
            if (!string.IsNullOrEmpty(rawContent))
            {
                return HttpUtility.HtmlEncode(rawContent);
            }
            return rawContent;
        }


        /// <summary>
        /// Url解码 
        /// </summary>
        /// <param name="urlToDecode"></param>
        /// <returns></returns>
        public static string UrlDecode(string urlToDecode)
        {
            if (!string.IsNullOrEmpty(urlToDecode))
            {
                return HttpUtility.UrlDecode(urlToDecode);
            }
            return urlToDecode;
        }

        /// <summary>
        /// Url编码 
        /// </summary>
        /// <param name="urlToEncode"></param>
        /// <returns></returns>
        public static string UrlEncode(string urlToEncode)
        {
            if (string.IsNullOrEmpty(urlToEncode))
            {
                return urlToEncode;
            }
            return HttpUtility.UrlEncode(urlToEncode).Replace("'", "%27");
        }

        public static string TextToHtml(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;
            return input.Replace("\r\n", "<br/>").Replace("\n", "<br/>").Replace("\r", "<br/>");
        }

    }
}
